package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.util.PageRequest;
import com.example.demo.service.UserService;
import com.example.demo.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:wjup
 * @Date: 2018/9/26 0026
 * @Time: 14:42
 */

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 测试 mybatis 的类
     * @return
     */
    @RequestMapping("/findUserTest")
    public List<User>  findUserTest(){
        User user = new User();
        user.setId(1);
        List<User> userList = userService.findUser(user);
        return userList;
    }


    @PostMapping(value="/findPage")
    public Object findPage(@RequestBody PageRequest pageQuery) {
        PageResult page = userService.findPage(pageQuery);
        return page;
    }


    @RequestMapping("/findUser")
    public List<User>  findUser(@RequestBody User user){
        List<User> userList = userService.findUser(user);
        return userList;
    }



    @RequestMapping("/getUserCount")
    public int getUserCount(){
        Map<String, Integer> parameterMap = new HashMap<String, Integer>();
        parameterMap.put("sexid", 0);
        parameterMap.put("usercount", -1);
        return userService.getUserCount(parameterMap);
    }

    @RequestMapping("")
    public String helloMybatis(){

        return "welcome mybatis!!!";
    }

    @RequestMapping("/getClass3/{id}")
    public String getClass3(@PathVariable int id){
        return userService.getClass3(id).toString();
    }

    @RequestMapping("/getClass/{id}")
    public String getClass(@PathVariable int id){
        return userService.getClass(id).toString();
    }

    @RequestMapping("/getUser/{id}")
    public String GetUser(@PathVariable int id){
        return userService.getUser(id).toString();
    }

    @RequestMapping("/addUser")
    public void addUser(){
        User user = new User("sadf","23");
        userService.addUser(user);
        System.out.println("adduser  ok");
//        User sel = userService.sel(id);
//        return userService.sel(id).toString();
    }

    @RequestMapping("addUserByAn")
    public String addUserByAn(){
        User user = new User("sadf","23");
        user.setRealName("sadf");
        userService.addUserByAn(user);
        System.out.println("addUserByAn  ok");
        return "addUserByAn  ok";
//        User sel = userService.sel(id); addUserByAn
//        return userService.sel(id).toString();
    }

    @RequestMapping("/delUser/{id}")
    public String delUser(@PathVariable int id){
        userService.delUser(id);
        System.out.println("delet  ok");
        return "delet  ok";
//        return userService.sel(id).toString();
    }

    @RequestMapping("/updateUser")
    public String updateUser(){
        User user = new User("sadf","23");
        user.setId(1);
        user.setPassWord("8789");
        userService.updateUser(user);
        return "updateUser  ok";
//        User sel = userService.sel(id);
//        return userService.sel(id).toString();
    }

    @RequestMapping("/getAllUser")
    public String getAllUser(){
        List<User> allUser = userService.getAllUser();
        System.out.println("getAllUser ok");
        return allUser.toString();
    }

    @RequestMapping("/getAllUserLike")
    public String getAllUserLike(){
        String name = "name";
        List<User> allUser = userService.getAllUserLike(name);
        System.out.println("getAllUser ok");
        return allUser.toString();
    }

}